package com.xuanxu.blogs.module.user.controller.app;

import cn.hutool.core.bean.BeanUtil;
import com.xuanxu.blogs.framework.common.enums.StatusEnum;
import com.xuanxu.blogs.module.user.config.UserProperties;
import com.xuanxu.blogs.module.user.entity.dao.UserDao;
import com.xuanxu.blogs.module.user.entity.vo.app.InsertUserRequestVo;
import com.xuanxu.blogs.module.user.entity.vo.app.SelectUserRequestVo;
import com.xuanxu.blogs.module.user.entity.vo.app.UpdateUserRequestVo;
import com.xuanxu.blogs.module.user.entity.vo.app.UserResponseVo;
import com.xuanxu.blogs.module.user.enums.UserTypeEnum;
import com.xuanxu.blogs.module.user.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author XuanXu
 * @version 1.0
 * @date 2024-08-19 15:10
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/app-api/user")
@Api(tags = "用户相关接口")
public class AppUserController {

    private final UserService userService;
    private final UserProperties userProperties;

    @PostMapping
    @ApiOperation("新增用户")
    public ResponseEntity<Object> insertUser(@RequestBody InsertUserRequestVo insertUserRequestVo) {

        UserDao userDao = BeanUtil.copyProperties(insertUserRequestVo, UserDao.class);
        userDao.setUserAvatar(userProperties.getDefaultUserAvatar());
        userDao.setUserStatus(StatusEnum.TRUE.getStatus());
        userDao.setUserType(UserTypeEnum.COMMON_USER.getType());

        boolean isSuccess = userService.insertUser(userDao);

        return ResponseEntity.ok(isSuccess);
    }

    @PutMapping
    @ApiOperation("更新用户")
    public ResponseEntity<Boolean> updateUserById(@RequestBody UpdateUserRequestVo updateUserRequestVo) {
        UserDao userDao = BeanUtil.copyProperties(updateUserRequestVo, UserDao.class);
        boolean isSuccess = userService.updateUserById(userDao);
        return ResponseEntity.ok(isSuccess);
    }

    @GetMapping
    @ApiOperation("查询用户")
    public ResponseEntity<List<UserResponseVo>> selectUser(SelectUserRequestVo selectUserRequestVo) {
        UserDao userDao = BeanUtil.copyProperties(selectUserRequestVo, UserDao.class);
        List<UserDao> userDaoList = userService.selectUser(userDao);
        List<UserResponseVo> userResponseVoList = BeanUtil.copyToList(userDaoList, UserResponseVo.class);
        return ResponseEntity.ok(userResponseVoList);
    }

    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "Integer", name = "userId", value = "用户ID", required = true)
    })
    @DeleteMapping("/{userId}")
    @ApiOperation("删除用户")
    public ResponseEntity<Boolean> deleteUserById(@PathVariable("userId") Long userId) {
        boolean isSuccess = userService.deleteUserById(userId);
        return ResponseEntity.ok(isSuccess);
    }

}
